//
//  SZChannelView.m
//  03-仿网易新闻
//
//  Created by ItHeiMa on 2017/1/3.
//  Copyright © 2017年 itHeima. All rights reserved.
//

#import "SZChannelView.h"
#import "SZChannelModel.h"
#import "SZChannelLabel.h"

//所有的视图默认tag都是0，所以不要把tag设为0，一般给一个baseTag
const int baseTag = 666;

@interface SZChannelView()

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation SZChannelView

- (id) initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame: frame]) {
        [self setupUI];
    }
    return self;
}


/**
 重写 channelList 的set方法，添加所有的频道的label

 @param channelList 频道的模型数组
 */
- (void) setChannelList:(NSArray *)channelList {
    //1. 遍历channelList， 创建并添加label， sizeToFit
    _channelList = channelList;
    //label的origin
    CGFloat origin = 0;
    //index，记录循环到第一次，用来给label设tag
    NSInteger index = 0;
    
    //循环添加label
    for (SZChannelModel *model in channelList) {
        //用便利构造器创建一个label，此时label已经有了宽高；
        SZChannelLabel *lable = [SZChannelLabel labelWithText:model.tname];
        //NSLog(@"%@", NSStringFromCGRect(lable.bounds));
        //计算label的frame
        CGRect frame = CGRectMake(origin, 0, lable.bounds.size.width, self.bounds.size.height);
        lable.frame = frame;
        [_scrollView addSubview:lable];
        
        //给lable加一个tag
        lable.tag = index + baseTag;
        
        //c>添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(channelClick:)];
        [lable addGestureRecognizer:tap];
        
        //如果是第一个label，初始化的时候，默认就把颜色设为0
        if (index == 0) lable.isSelected = YES;
        
        //origin加上当前lable的宽度后，就成了下一个label的origin
        origin += lable.bounds.size.width;
        
        index += 1;
    }
    
    //设置scrollView的contentSize
    _scrollView.contentSize = CGSizeMake(origin, self.bounds.size.height);
}


/**
 重写currentIndex的set方法
 */
- (void) setCurrentIndex:(NSInteger)currentIndex {
    //如果是同一个index， 什么也不做
    if (_currentIndex == currentIndex) return;
    
    //1. a>找到对应的currentIndex的lable，把它的颜色和字体改成高亮
    NSInteger tag = currentIndex + baseTag;
    SZChannelLabel *label = [_scrollView viewWithTag:tag];
    label.isSelected = YES;
    
    //b>看其是否需要调整位置
    CGFloat lx = label.frame.origin.x;
    CGFloat lw = label.frame.size.width + lx;
    CGFloat cx = _scrollView.contentOffset.x;
    //如果label的左边(originx)比contentOffset的x要小说明，这个label处在左边的屏幕的外面
    //如果label的右边（originx+width）比contentOffset的x要大；说明这个label处在右边的屏幕的外面
    if (lx < cx || lw > cx) {
        //将选中的label滚动到可视区域
        [_scrollView scrollRectToVisible:label.frame animated:YES];
    }
    
    //2. 把之前高亮的label改成正常的状态
    NSInteger oldTag = _currentIndex + baseTag;
    SZChannelLabel *oldlabel = [_scrollView viewWithTag:oldTag];
    oldlabel.isSelected = NO;
    
    //3. 修改_currentIndex所值
    _currentIndex = currentIndex;
}

/**
 label点击事件

 @param tap 点击手势
 */
- (void) channelClick:(UITapGestureRecognizer *) tap {
    //获取点击的label
    SZChannelLabel *label = (SZChannelLabel *)tap.view;
    NSInteger index = label.tag - baseTag;
    //设置currentIndex
    [self setCurrentIndex:index];
    
    //让delegate实现点击事件
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(channelChanged:)]) {
        [self.delegate channelChanged:index];
    }
}

/**
 设置UI
 */
- (void) setupUI {
    _scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_scrollView];
}

@end
